package com.kun.user.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.api.IUserMoneyLogService;
import com.kun.api.IUserService;
import com.kun.common.constant.ShopCode;
import com.kun.common.exception.CustomerException;
import com.kun.pojo.domain.TradeUser;
import com.kun.pojo.domain.TradeUserMoneyLog;
import com.kun.pojo.entity.Result;
import com.kun.user.mapper.TradeUserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
@DubboService(interfaceClass = IUserService.class)
public class TradeUserServiceImpl extends ServiceImpl<TradeUserMapper, TradeUser> implements IUserService {

    @Autowired
    private IUserMoneyLogService userMoneyLogService;

    /**
     * 根据用户ID查询
     */
    @Override
    public TradeUser findOne(Long userId) {
        if (userId == null) throw new CustomerException(ShopCode.SHOP_USER_IS_NULL);
        return getById(userId);
    }

    /**
     * 扣减余额，记录日志
     */
    @Override
    public Result reduceMoneyPaid(TradeUserMoneyLog tradeUserMoneyLog) {
        // 校验参数
        if (tradeUserMoneyLog.getUseMoney().compareTo(BigDecimal.ZERO) <= 0)
            throw new CustomerException(ShopCode.SHOP_USER_IS_NULL);
        // 查询订单使用日志
        TradeUserMoneyLog tradeUserMoneyLog1 = userMoneyLogService.lambdaQuery()
                .eq(TradeUserMoneyLog::getUserId, tradeUserMoneyLog.getUserId())
                .eq(TradeUserMoneyLog::getOrderId, tradeUserMoneyLog.getOrderId()).last("LIMIT 1").one();

        TradeUser tradeUser = findOne(tradeUserMoneyLog.getUserId());

        if (tradeUserMoneyLog.getMoneyLogType().equals(ShopCode.SHOP_USER_MONEY_PAID.getCode())) {
            // 付款操作
            if (tradeUserMoneyLog1 != null)
                // 已经付款
                throw new CustomerException(ShopCode.SHOP_ORDER_PAY_STATUS_IS_PAY);
            // 获取购买商品之后的余额
            int i = new BigDecimal(tradeUser.getUserMoney()).subtract(tradeUserMoneyLog.getUseMoney()).intValue();
            lambdaUpdate().set(TradeUser::getUserMoney, i).eq(TradeUser::getUserId, tradeUser.getUserId()).update();
        }
        if (tradeUserMoneyLog.getMoneyLogType().equals(ShopCode.SHOP_USER_MONEY_REFUND.getCode())) {
            // 退款操作
            if (tradeUserMoneyLog1 == null) throw new CustomerException(ShopCode.SHOP_ORDER_PAY_STATUS_NO_PAY);
            // 防止多次退款
            TradeUserMoneyLog moneyLog = userMoneyLogService.lambdaQuery()
                    .eq(TradeUserMoneyLog::getOrderId, tradeUserMoneyLog.getOrderId())
                    .eq(TradeUserMoneyLog::getUserId, tradeUserMoneyLog.getUserId())
                    .eq(TradeUserMoneyLog::getMoneyLogType, ShopCode.SHOP_USER_MONEY_REFUND.getCode())
                    .last("LIMIT 1").one();
            if (moneyLog != null) throw new CustomerException(ShopCode.SHOP_USER_MONEY_REFUND_ALREADY);
            // 回退余额
            int i = new BigDecimal(tradeUser.getUserMoney()).add(tradeUserMoneyLog.getUseMoney()).intValue();
            lambdaUpdate().set(TradeUser::getUserMoney, i).eq(TradeUser::getUserId, tradeUser.getUserId()).update();
        }
        // 记录订单日志
        tradeUserMoneyLog.setCreateTime(new Date());
        userMoneyLogService.save(tradeUserMoneyLog);
        return Result.success(ShopCode.SHOP_SUCCESS.getMessage());
    }
}




